package com.dili.dd.gpi.aop;

import com.dili.dd.gpi.util.spring.DataSourceLookupKey;
import com.dili.dd.gpi.util.spring.MultipleDataSource;
import com.dili.dd.gpi.util.spring.RequireDataSource;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MultipleDataSourceAspectAdvice {

    private static Logger log = LoggerFactory.getLogger(MultipleDataSourceAspectAdvice.class);

    public void determineDataSource(JoinPoint jp) throws Throwable {
        RequireDataSource requireDataSource = ((MethodSignature) jp.getSignature())
                .getMethod().getAnnotation(RequireDataSource.class);
        DataSourceLookupKey lookupKey = null;
        if(requireDataSource == null) {
            lookupKey = DataSourceLookupKey.GPI_DATASOURCE;
        }else{
            lookupKey = requireDataSource.key();
        }
        log.info("====>" + lookupKey);
        MultipleDataSource.setDataSourceKey(lookupKey);
    }
}